package com.ayoka.parenting.ui.group.detail;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.CircleGroupBean;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.bean.TopicBean;
import com.ayoka.parenting.http.API;
import com.ayoka.parenting.http.HttpBeanInterceptCallback;
import com.ayoka.parenting.interf.CallbackUtil;
import com.ayoka.parenting.interf.RequestCallback;
import com.ayoka.parenting.mvp.BasePresenter;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GroupDetailModel&.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GroupDetailModel implements GroupDetailContract.Model{

    @Override
    public void getCircleGroupDetail(final BasePresenter presenter, final String circleGroupId, final RequestCallback<CircleGroupBean> callback) {
        API.getCircleGroupDetail(presenter, circleGroupId, new HttpBeanInterceptCallback<BaseResponse<CircleGroupBean>>() {
            @Override
            public void onSuccess(BaseResponse<CircleGroupBean> response) {
                CallbackUtil.notifySuccess(callback, response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    getCircleGroupDetail(presenter, circleGroupId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void getCircleGroupTopicListData(final BasePresenter presenter, final String circleGroupId, final int pageNo, final int pageSize, final RequestCallback<PagerBean<TopicBean>> callback) {
        API.getCircleGroupTopicListData(presenter, circleGroupId, pageNo, pageSize, new HttpBeanInterceptCallback<BaseResponse<PagerBean<TopicBean>>>() {
            @Override
            public void onSuccess(BaseResponse<PagerBean<TopicBean>> response) {
                CallbackUtil.notifySuccess(callback, response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    getCircleGroupTopicListData(presenter, circleGroupId, pageNo, pageSize, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void joinCircle(final BasePresenter presenter, final String circleGroupId, final RequestCallback<BaseResponse> callback) {
        API.joinCircle(presenter, circleGroupId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    joinCircle(presenter, circleGroupId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void quitCircle(final BasePresenter presenter, final String circleGroupId, final RequestCallback<BaseResponse> callback) {
        API.quitCircle(presenter, circleGroupId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    quitCircle(presenter, circleGroupId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void praiseTopic(final BasePresenter presenter, final String topicId, final RequestCallback<BaseResponse> callback) {
        API.praiseTopic(presenter, topicId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    praiseTopic(presenter, topicId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void cancelPraiseTopic(final BasePresenter presenter, final String topicId, final RequestCallback<BaseResponse> callback) {
        API.cancelPraiseTopic(presenter, topicId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    cancelPraiseTopic(presenter, topicId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void deleteTopic(final BasePresenter presenter, final String topicId, final RequestCallback<BaseResponse> callback) {
        API.deleteTopic(presenter, topicId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    deleteTopic(presenter, topicId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void topTopic(final BasePresenter presenter, final String topicId, final RequestCallback<BaseResponse> callback) {
        API.topTopic(presenter, topicId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    topTopic(presenter, topicId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void cancelTopTopic(final BasePresenter presenter, final String topicId, final RequestCallback<BaseResponse> callback) {
        API.cancelTopTopic(presenter, topicId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    cancelTopTopic(presenter, topicId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }
}
